package com.software.loginregister.fragment.homecategoryfragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.software.loginregister.R;
import com.software.loginregister.adapter.RestaurantAdapter;
import com.software.loginregister.entity.Restaurant;
import com.software.loginregister.header.HealthyFoodHeader;

import java.util.ArrayList;
import java.util.List;

public class EatOutFragment extends Fragment {
    private RecyclerView rvRestaurants;
    private List<Restaurant> restaurantList;
    private List<String> healthyFoodList;
    private RestaurantAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_eat_out, container, false);
        initViews(view);
        initData();
        setupAdapters();
        return view;
    }

    private void initViews(View view) {
        rvRestaurants = view.findViewById(R.id.rv_restaurants);
        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout);

        // 设置下拉刷新的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.theme_color);
        // 设置下拉刷新的监听器
        swipeRefreshLayout.setOnRefreshListener(this::refreshData);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setAutoMeasureEnabled(true);
        rvRestaurants.setLayoutManager(layoutManager);
        rvRestaurants.setNestedScrollingEnabled(true);
    }

    private void refreshData() {
        // 模拟刷新数据
        initData();
        setupAdapters();
        // 结束刷新动画
        swipeRefreshLayout.setRefreshing(false);
    }

    private void initData() {
        // 初始化健康食品列表
        healthyFoodList = new ArrayList<>();
        healthyFoodList.add("1. 胡氏大盘鸡\n大盘鸡主要采用炒和炖的烹饪方式......");
        healthyFoodList.add("2. 兰州拉面\n兰州拉面要采用最经典的烹饪方式......");
        healthyFoodList.add("3. 香河肉饼\n采用传统工艺制作......");

        // 初始化餐厅列表
        restaurantList = new ArrayList<>();
        // 添加多个餐厅
        restaurantList.add(new Restaurant(
                "老张家瓦罐肉 (砂锅....",
                "4.8",
                "月销50+",
                R.drawable.img_restaurant
        ));
        restaurantList.add(new Restaurant(
                "张家香河肉饼 (裕华区....",
                "4.8",
                "月销500+",
                R.drawable.img_restaurant
        ));
        restaurantList.add(new Restaurant(
                "刘小六小龙虾自助 (新....",
                "4.8",
                "月销300+",
                R.drawable.img_restaurant
        ));
        restaurantList.add(new Restaurant(
                "金百万烤鸭店 (中山....",
                "4.7",
                "月销200+",
                R.drawable.img_restaurant
        ));
        restaurantList.add(new Restaurant(
                "川味小馆 (裕华....",
                "4.9",
                "月销150+",
                R.drawable.img_restaurant
        ));
    }

    private void setupAdapters() {
        List<Object> items = new ArrayList<>();
        items.add(new HealthyFoodHeader(healthyFoodList));// 添加健康饮食卡片数据
        items.addAll(restaurantList); // 添加餐厅列表数据
        rvRestaurants.setAdapter(adapter);
        adapter = new RestaurantAdapter(items);
        rvRestaurants.setAdapter(adapter);
    }


}